package com.zxp.zxpsmarthomeapk.view.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;

import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.popup.QMUIListPopup;
import com.qmuiteam.qmui.widget.popup.QMUIPopup;
import com.zxp.zxpsmarthomeapk.R;
import com.zxp.zxpsmarthomeapk.base.CommentActivity;
import com.zxp.zxpsmarthomeapk.util.AnimUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class RoomDeviceActivity extends CommentActivity {

    /**
     * 标题栏
     */
    @BindView(R.id.topbar_room_device)
    QMUITopBar topbar_room_device;
    /**
     * 门锁
     */
    @BindView(R.id.cv_doorclock)
    CardView cv_door_clock;
    /**
     * 灯光
     */
    @BindView(R.id.cv_light)
    CardView cv_light;
    /**
     * 空调
     */
    @BindView(R.id.cv_air)
    CardView cv_air;
    /**
     * 窗帘
     */
    @BindView(R.id.cv_curtain)
    CardView cv_curtain;
    /**
     * 当前室温
     */
    @BindView(R.id.tv_temperature_indoor)
    TextView tv_temperature_indoor;
    /**
     * 设定温度
     */
    @BindView(R.id.tv_temperature_setting)
    TextView tv_temperature_setting;
    /**
     * 温度+
     */
    @BindView(R.id.iv_tem_inc)
    ImageView iv_tem_inc;
    /**
     * 温度-
     */
    @BindView(R.id.iv_tem_dec)
    ImageView iv_tem_dec;
    /**
     * 风量
     */
    @BindView(R.id.tv_speed_level)
    TextView tv_speed_level;
    /**
     * 风量控制
     */
    @BindView(R.id.iv_speed_dec)
    ImageView iv_speed_dec;
    /**
     * 风量控制
     */
    @BindView(R.id.iv_speed_inc)
    ImageView iv_speed_inc;
    /**
     * 风速图标
     */
    @BindView(R.id.iv_speed)
    ImageView iv_speed;
    /**
     * 吹风模式
     */
    @BindView(R.id.tv_air_mode)
    TextView tv_air_mode;
    /**
     * 更多模式
     */
    @BindView(R.id.iv_right)
    ImageView iv_right;
    /**
     * 制冷
     */
    @BindView(R.id.cv_make_cold)
    CardView cv_make_cold;
    @BindView(R.id.iv_make_cold)
    ImageView iv_make_cold;
    /**
     * 制热
     */
    @BindView(R.id.cv_make_hot)
    CardView cv_make_hot;
    @BindView(R.id.iv_make_hot)
    ImageView iv_make_hot;
    /**
     * 循环
     */
    @BindView(R.id.cv_make_loop)
    CardView cv_make_loop;
    @BindView(R.id.iv_make_loop)
    ImageView iv_make_loop;
    /**
     * 关机
     */
    @BindView(R.id.cv_make_close)
    CardView cv_make_close;
    /**
     * 门锁开关
     */
    @BindView(R.id.switch_door)
    Switch switch_door;
    /**
     * 灯光开关
     */
    @BindView(R.id.switch_light)
    Switch switch_light;
    /**
     * 空调开关
     */
    @BindView(R.id.switch_conditioner)
    Switch switch_conditioner;
    /**
     * 窗帘开关
     */
    @BindView(R.id.switch_cuitain)
    Switch switch_curtain;
    /**
     * 灯光显示控制
     */
    @BindView(R.id.seek_bar_light)
    SeekBar seek_bar_light;

    @BindView(R.id.ll_conditioner)
    LinearLayout ll_conditioner;
    @BindView(R.id.ll_curtain)
    LinearLayout ll_curtain;

    @BindView(R.id.tv_room_title)
    TextView tv_room_title;

    private int tv_temperature_setting_default = 15;
    private int tv_speedlevel_default = 0;
    private QMUIListPopup mListPopup;
    private int openColdWindFlag = 0;     //制冷打开标志
    private int openHotWindFlag = 0;     //制热打开标志
    private int openLoopWindFlag = 0;     //循环打开标志

    private static final int MSG_INIT_ANIM = 0;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_INIT_ANIM:
                    playViewAnim();
                    break;
            }
        }
    };

    private void playViewAnim() {
        AnimUtils.playViewAnim(this, cv_door_clock, R.anim.bottom_up_in, 0, 100);
        AnimUtils.playViewAnim(this, cv_light, R.anim.bottom_up_in, 0, 200);
        AnimUtils.playViewAnim(this, cv_air, R.anim.bottom_up_in, 0, 300);
        AnimUtils.playViewAnim(this, cv_curtain, R.anim.bottom_up_in, 0, 400);
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_room_device;
    }

    @Override
    protected void initData() {
        Bundle bundle = getIntent().getExtras();
        tv_room_title.setText(bundle.getString("msg"));
    }

    @Override
    protected void initView() {
        topbar_room_device.setTitle("Control Board");

        topbar_room_device.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        topbar_room_device.addRightImageButton(R.drawable.ic_settings_black_24dp, R.id.topbar_right_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toast("显示设置界面");
            }
        });

        switch_door.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    toast("开门");
                } else {
                    toast("关门");
                }
            }
        });

        switch_light.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    seek_bar_light.setClickable(true);
                    seek_bar_light.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                        @Override
                        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

                        }

                        @Override
                        public void onStartTrackingTouch(SeekBar seekBar) {

                        }

                        @Override
                        public void onStopTrackingTouch(SeekBar seekBar) {

                        }
                    });
                } else {
                    seek_bar_light.setClickable(false);
                }
            }
        });

        switch_conditioner.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    ll_conditioner.setVisibility(View.VISIBLE);
                } else {
                    ll_conditioner.setVisibility(View.GONE);
                }
            }
        });

        switch_curtain.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    ll_curtain.setVisibility(View.VISIBLE);
                } else {
                    ll_curtain.setVisibility(View.GONE);
                }
            }
        });
    }

    @OnClick({R.id.iv_tem_inc, R.id.iv_tem_dec})
    void changeTemperature(View view) {
        switch (view.getId()) {
            case R.id.iv_tem_inc:
                tv_temperature_setting_default++;
                if (tv_temperature_setting_default > 30) {
                    tv_temperature_setting_default = 15;
                }
                break;
            case R.id.iv_tem_dec:
                tv_temperature_setting_default--;
                if (tv_temperature_setting_default < 15) {
                    tv_temperature_setting_default = 30;
                }
                break;
            default:
                break;
        }
        tv_temperature_setting.setText(tv_temperature_setting_default + "");
    }

    @OnClick({R.id.iv_speed_inc, R.id.iv_speed_dec})
    void changeSpeedlevel(View view) {
        switch (view.getId()) {
            case R.id.iv_speed_inc:
                tv_speedlevel_default++;
                if (tv_speedlevel_default > 7) {
                    tv_speedlevel_default = 0;
                }
                break;
            case R.id.iv_speed_dec:
                tv_speedlevel_default--;
                if (tv_speedlevel_default < 0) {
                    tv_speedlevel_default = 7;
                }
                break;
            default:
                break;
        }
        tv_speed_level.setText(tv_speedlevel_default + "");

        if (tv_speedlevel_default != 0) {
            iv_speed.setImageResource(R.mipmap.wind_speed_dn);
        } else {
            iv_speed.setImageResource(R.mipmap.wind_speed_up);
        }
    }

    @OnClick({R.id.iv_right})
    void showMoremode(View view) {
        initListPopupIfNeed();
        mListPopup.setAnimStyle(QMUIPopup.ANIM_GROW_FROM_CENTER);
        mListPopup.setPreferredDirection(QMUIPopup.DIRECTION_TOP);
        mListPopup.show(view);
    }

    private void initListPopupIfNeed() {
        if (mListPopup == null) {
            final String[] listItems = new String[]{
                    "自动",
                    "mode1",
                    "mode2",
                    "mode3",
                    "mode4",
                    "mode5",
            };
            List<String> data = new ArrayList<>();
            Collections.addAll(data, listItems);
            ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.simple_list_item, data);
            mListPopup = new QMUIListPopup(this, QMUIPopup.DIRECTION_NONE, adapter);
            mListPopup.create(QMUIDisplayHelper.dp2px(this, 250), QMUIDisplayHelper.dp2px(this, 200), new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    tv_air_mode.setText(listItems[i]);
                    mListPopup.dismiss();
                }
            });
            mListPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    mListPopup.dismiss();
                }
            });
        }
    }

    @OnClick({R.id.cv_make_cold})
    void makeCold() {
        if (openColdWindFlag == 0) {
            iv_make_cold.setVisibility(View.VISIBLE);
            openColdWindFlag = 1;
        } else {
            iv_make_cold.setVisibility(View.INVISIBLE);
            openColdWindFlag = 0;
        }
    }

    @OnClick({R.id.cv_make_hot})
    void makeHot() {
        if (openHotWindFlag == 0) {
            iv_make_hot.setVisibility(View.VISIBLE);
            openHotWindFlag = 1;
        } else {
            iv_make_hot.setVisibility(View.INVISIBLE);
            openHotWindFlag = 0;
        }
    }

    @OnClick({R.id.cv_make_loop})
    void makeLoop() {
        if (openLoopWindFlag == 0) {
            iv_make_loop.setVisibility(View.VISIBLE);
            openLoopWindFlag = 1;
        } else {
            iv_make_loop.setVisibility(View.INVISIBLE);
            openLoopWindFlag = 0;
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        mHandler.sendEmptyMessageDelayed(MSG_INIT_ANIM, 200);
    }
}
